using System;
using System.Diagnostics;
using System.IO;

namespace CoverageRunner
{
    public class UnitTestRunner
    {
        private readonly string executable;
        private readonly string arguments;
        private int exitCode;
        
        public UnitTestRunner(string executable, string arguments)
        {
            if (!File.Exists(executable))
                throw new ApplicationException(string.Format("Unable to find the specified unit test runner executable '{0}'", executable));

            this.executable = executable;
            this.arguments = arguments;
        }

        public int ExitCode
        {
            get { return exitCode; }
        }

        public string Execute()
        {
            Process processRunner = new Process();
            
            processRunner.StartInfo.UseShellExecute = false;
            processRunner.StartInfo.RedirectStandardOutput = true;
            processRunner.StartInfo.FileName = executable;
            processRunner.StartInfo.Arguments = arguments;
            processRunner.Start();
            processRunner.WaitForExit();
            string output = processRunner.StandardOutput.ReadToEnd();
            exitCode = processRunner.ExitCode;
            return output;
        }       
    }
}